﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using HugeFlow.Utility;

namespace HugeFlow.CommandPattern.Interactivity
{
    /// <summary>
    /// Trigger for Loaded Event
    /// </summary>
    public class LoadedEventTrigger : EventTriggerBase<DependencyObject>
    {
        protected override string GetEventName()
        {
            return "Loaded";
        }

        protected override void OnSourceChanged(DependencyObject oldSource, DependencyObject newSource)
        {
            base.OnSourceChanged(oldSource, newSource);

            if (oldSource is FrameworkElement)
                (oldSource as FrameworkElement).Loaded -= new RoutedEventHandler(LoadedEventTrigger_Loaded);

            if (newSource is FrameworkElement)
                (newSource as FrameworkElement).Loaded += new RoutedEventHandler(LoadedEventTrigger_Loaded);

        }

        protected override void OnDetaching()
        {
            (AssociatedObject as FrameworkElement).Loaded -= new RoutedEventHandler(LoadedEventTrigger_Loaded);

            base.OnDetaching();
        }

        void LoadedEventTrigger_Loaded(object sender, RoutedEventArgs e)
        {
            base.InvokeActions(e);
        }
    }
}
